package com.dev.module_zqc_sleep.activity

import android.app.AlarmManager
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.dev.module_zqc_sleep.broadcast.AlarmReceiver

class AlarmStopActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val alarmId = intent.getIntExtra("alarm_id", -1)
        if (alarmId != -1) {
            AlarmReceiver.mediaPlayer?.stop()
            AlarmReceiver.mediaPlayer?.release()
            AlarmReceiver.mediaPlayer = null

            val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
            val cancelIntent = Intent(this, AlarmReceiver::class.java)
            val cancelPendingIntent = PendingIntent.getBroadcast(this, alarmId, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
            alarmManager.cancel(cancelPendingIntent)

            val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.cancel(alarmId)
        }

        finish()
    }
}

